/*业主相关JS*/

var urlPath = window.location.pathname;

$(".m_left li a").each(function(){
	if($(this).attr("href")==urlPath)
	{
		$(this).parent().attr("class","menu_select");
	}
});

$("body").click(function(){
	if($(".city").css("display")!="none")
	{
		$(".city").hide();
	}
});

if($(".city").length>0)
{
	$(".city a").each(function(){
		$(this).click(function(){
			selCity(this);
		});
	});
}

function initUserPwd(){
	$("#txt_username").val("附属卡号或用户名");
	$("#txt_username").css("color","#B4B4B4");
	$("#txt_password1").val("密码");
	$("#txt_password1").css("color","#B4B4B4");
	$("#txt_checkcode").val("验证码");
	$("#txt_checkcode").css("color","#B4B4B4");
	
	$("#txt_username").bind("focus",function(){
		if($("#txt_username").val()=="附属卡号或用户名")
		{
			$("#txt_username").val("");
			$("#txt_username").css("color","");
		}
	}).bind("blur",function(){
		if($("#txt_username").val()=="")
		{
			$("#txt_username").css("color","#B4B4B4");
			$("#txt_username").val("附属卡号或用户名");
		}
	});
	
	$("#txt_password1").bind("focus",function(){
		if($("#txt_password1").val()=="密码")
		{
			$("#txt_password1").hide();
			$("#txt_password").val("");
			$("#txt_password").show();
			$("#txt_password").focus();
		}
	});
	$("#txt_password").bind("blur",function(){
		if($("#txt_password").val()=="")
		{
			$("#txt_password1").show();
			$("#txt_password").hide();
		}
	});
	
	$("#txt_checkcode").bind("focus",function(){
		if($("#txt_checkcode").val()=="验证码")
		{
			$("#txt_checkcode").val("");
			$("#txt_checkcode").css("color","");
		}
	}).bind("blur",function(){
		if($("#txt_checkcode").val()=="")
		{
			$("#txt_checkcode").css("color","#B4B4B4");
			$("#txt_checkcode").val("验证码");
		}
	});
}

function selCity(obj)
{
	var cityName = obj;

	if(typeof(obj)!="string")
	{
		cityName = $(obj).html();
		if(cityName!="更多城市")
		{
			cityName +="市";
		}
	}
	if(cityName=="更多城市")
	{
		return;
	}
	setCookie("city_name",encodeURIComponent(cityName),30);
	window.location.reload();
}

function getCookie(c_name){
　　　if (document.cookie.length>0){
　　　　　c_start=document.cookie.indexOf(c_name + "=");
　　　　　if (c_start!=-1){
　　　　　　　c_start=c_start + c_name.length+1;
　　　　　　　c_end=document.cookie.indexOf(";",c_start);
       		if (c_end==-1) {
       			c_end=document.cookie.length;
       		}
        	return document.cookie.substring(c_start,c_end);
		 } 
	 }
　　   return "";
}

function setCookie(c_name, value, expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie=c_name+ "=" + value + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function showCity(evt)
{
	$(".city").show();
	var e=(evt)?evt:window.event;
	if (window.event) {
		e.cancelBubble=true;
	} else {
		e.stopPropagation();
	} 
}

function submitForm(formID)
{
	$("#"+formID).submit();
}

function codeReload(){
	var cpt = document.getElementById('codepic');
	cpt.src	= '/index/code/?t='+Math.random();
}

function winPosition()
{
	var l=(document.body.clientWidth-435)/2+$(document).scrollLeft();   
	var t=(document.body.clientHeight-280)/2+$(document).scrollTop();
	$(".owner_win").css("left",l);
	$(".owner_win").css("top",t);
	$(".owner_win").show();
}

function showWin(f1,f2)
{
	$("#sp_title").html("");
	$("#hidStatus").val("");
	eval(f1+"()");
	winPosition();
	$("#btn_save").unbind("click").bind("click",function(){eval(f2+"()");});
}

function openStatusWin()
{
	var type = 1;
	if($(".owner_info_status").html()=="正常")
	{
		type = 2;
		$("#sp_title").html("冻结一卡通(无法进行任何交易)");
	}
	else{
		type =1;
		$("#sp_title").html("激活一卡通(开启交易功能)");
	}

	//$("#sp_cancel_jifen").html(c_m);
	$("#hidStatus").val(type);
}

function statusOp()
{
	if($("#txt_pwd").val()=="")
	{
		alert("请输入密码");
		return;
	}
	
	var type = $("#hidStatus").val();
	var pwd = $("#txt_pwd").val();
	
	$.ajax({
	      type: "POST",
		  data:{"type":type,"pwd":pwd,"isAjax":1},
	      url: "/owner/index/op_status",
	      success: function(data) {
				if(data == -1000){
					window.location.href="/";
				}else if(data==-1){
					alert("密码错误");
				}
				else if(data==-2){
					alert("修改失败");
				}
				else if(data==1)
				{
					alert("操作成功");
					window.location.reload();
				}
				else{
					alert("操作失败");
				}
	      },
		  error:function(){
	    	  alert("发生错误,请重试");
		  }
	});
}

function changeCardStatus()
{
	var type=1;
	if($("#hidCardStatus").val()==1)
	{
		type = 2;
		$("#sp_title").html("关闭扣减积分");
	}
	else{
		$("#sp_title").html("开通扣减积分");
	}
	
	//$("#sp_cancel_jifen").html(c_m);
	$("#hidStatus").val(type);
}

function changeCardStatusOp()
{
	if($("#txt_pwd").val()=="")
	{
		alert("请输入密码");
		return;
	}
	
	var type = $("#hidStatus").val();
	var shopID = $("#hidShopID").val();
	var pwd = $("#txt_pwd").val();
	
	$.ajax({
	      type: "POST",
		  data:{"type":type,"pwd":pwd,"shopID":shopID,"isAjax":1},
	      url: "/owner/pay/op_status",
	      success: function(data) {
				if(data == -1000){
					window.location.href="/";
				}else if(data==-1){
					alert("密码错误");
				}
				else if(data==-2){
					alert("修改失败");
				}
				else if(data==1)
				{
					alert("操作成功");
					window.location.reload();
				}
	      },
		  error:function(){
	    	  alert("发生错误,请重试");
		  }
	});
}

function checkLogin(){
	if(arguments.length==1)
	{
		var keys=arguments[0];
		if(keys.keyCode!=13)
		{
			return false;
		}
	}
	
	var username = $('#txt_username').val();
	var password = $('#txt_password').val();
	var checkcode = $('#txt_checkcode').val();
	var cstr = '/\w+/';
	if(username==''){
		alert('用户名不能为空！');
		return false;
	}
	else if(password==''){
		alert('密码不能为空！');
		return false;
	}
	else if(checkcode==''){
		alert('验证码不能为空！');
		return false;
	}
	
	$('#txt_username').val($.trim(username));
	var flag = true;
    $.ajax({
        type : "post",  
        url : "/index/userLogin",  
        data : { txt_checkcode: checkcode,user_name:username, pwd:password},  
        async : false,  
        success : function(data){  
        	if(data == -1){
        		alert('验证码错误！');
    			codeReload();
    			return false;
    		}
        	else if(data == -2){
        		alert('用户名或密码错误！');
    			return false;
        	}
        	else if(data == 1){
        		window.location.href="/owner/index/first_login";
        	}
        	else if(data==2)
        	{
        		window.location.href="/owner/index/show";
        	}
        }  
    });  
}

function showBefore(shopID,cardStatus)
{
	$("#hidShopID").val(shopID);
	$("#hidCardStatus").val(cardStatus);
}

/*复选框全选*/
function checkBoxSelectAll(obj) {
	var elems = document.getElementById("listTab").getElementsByTagName("INPUT");

	for ( var i = 0; i < elems.length; i++) {
		if (elems[i].name == "checkboxes" || elems[i].name == "checkboxes[]") {
			elems[i].checked = obj.checked;
		}
	}
}

/*全选操作*/
function betchChangeCardStatus(type) {
	var str = "";
	$(".idsClass").each(function() {
		if ($(this).attr("checked") == "checked") {
			str += $(this).val() + ",";
		}
	})
	if (str == "") {
		alert("请至少选择一条记录");
		return;
	}

	str = str.substring(0, str.length - 1);

	if(type==1)
	{
		$("#sp_title").html("开启扣减积分功能");
	}
	else{
		$("#sp_title").html("关闭扣减积分功能");
	}

	winPosition();
	
	$("#btn_save").unbind("click").bind("click",function(){betchChangeOp(str,type);});
}

function betchChangeOp(ids,type)
{
	var pwd = $("#txt_pwd").val();
	
	if(pwd=="")
	{
		alert("请输入密码");
		return;
	}
	
	$.ajax({
	      type: "POST",
		  data:{"type":type,"pwd":pwd,"ids":ids,"isAjax":1},
	      url: "/owner/shop/betchChange",
	      success: function(data) {
				if(data == -1000){
					window.location.href="/";
				}else if(data==-1){
					alert("密码错误");
				}
				else if(data==-2){
					alert("修改失败");
				}
				else if(data==1)
				{
					alert("操作成功");
					window.location.reload();
				}
	      },
		  error:function(){
	    	  alert("发生错误,请重试");
		  }
	});
}

function opAllStatus(type)
{
	winPosition();
	if(type==1)
	{
		$("#sp_title").html("开启全部增加/扣减积分功能");
	}
	else{
		$("#sp_title").html("关闭全部增加/扣减积分功能");
	}
	$("#btn_save").unbind("click").bind("click",function(){ opStatus(type);});
}

function opStatus(type)
{
	var pwd = $("#txt_pwd").val();
	
	if(pwd=="")
	{
		alert("请输入密码");
		return;
	}
	
	$.ajax({
	      type: "POST",
		  data:{"type":type,"pwd":pwd,"isAjax":1},
	      url: "/owner/setting/changeStatus",
	      success: function(data) {
				if(data == -1000){
					window.location.href="/";
				}else if(data==-1){
					alert("密码错误");
				}
				else if(data==-2){
					alert("修改失败");
				}
				else if(data==1)
				{
					alert("操作成功");
					window.location.reload();
				}
	      },
		  error:function(){
	    	  alert("发生错误,请重试");
		  }
	});
}

function changeUsePayPwd(type)
{
	winPosition();
	if(type==1)
	{
		$("#sp_title").html("开启使用账户密码");
	}
	else{
		$("#sp_title").html("关闭使用账户密码");
	}
	$("#btn_save").unbind("click").bind("click",function(){ changeUsePayPwdOp(type);});
}

function changeUsePayPwdOp(type)
{
	var pwd = $("#txt_pwd").val();
	
	if(pwd=="")
	{
		alert("请输入密码");
		return;
	}
	
	$.ajax({
	      type: "POST",
		  data:{"type":type,"pwd":pwd,"isAjax":1},
	      url: "/owner/setting/changeUsePayPwd",
	      success: function(data) {
				if(data == -1000){
					window.location.href="/";
				}else if(data==-1){
					alert("密码错误");
				}
				else if(data==-2){
					alert("修改失败");
				}
				else if(data==1)
				{
					alert("操作成功");
					window.location.reload();
				}
	      },
		  error:function(){
	    	  alert("发生错误,请重试");
		  }
	});
}

var cityExistList = [];
function initCityList()
{
	var provinces = new Array("北京市","天津市","河北省","山西省","内蒙古","辽宁省","吉林省",
			"黑龙江省","上海市","江苏省","浙江省","安徽省","福建省","江西省","山东省","河南省",
			'湖北省','湖南省','广东省','广西','海南省','重庆市','四川省','贵州省','云南省','西藏',
			'陕西省','甘肃省','青海省','宁夏','新疆');
	
	var p_t = "";
	var c_t = "";
	var cityCss = "";
	var cityHtml = "<ul>";
	
	var ii=0;
	var wordsum = 0;
	var pl=provinces.length;
	for(k=0;k<pl;k++)
	{
		p_t = provinces[k];
		cityHtml += "<li><span>"+p_t+"</span><table><tr>";
		
		ii=0;
		wordsum = 0;
		for (i = 0; i < lmcount; i++) {
			c_t = lm[i][0];
			if (lm[i][1] == p_t) {
				cityCss = "";
				for(j=0; j<cityExistList.length; j++)
				{
					if(cityExistList[j]==c_t)
					{
						cityCss = " class='font_w_b' ";
					}
				}
				wordsum += c_t.length; 
				ii++;
				
				cityHtml += "<td align='left'><a href='javascript:selCity(\""+c_t+"\")'"+cityCss+">"+c_t+"</a></td>";
				if((ii%7==0 && ii>0) || wordsum>=32)
				{
					cityHtml += "</tr></table><table class='city_t'><tr>";
					wordsum = 0;
					ii = 0;
				}
			}
			
		}
		
		cityHtml += "</tr></table></li>";
		
	}
	
	cityHtml += "</ul>";
	
	
	$(".city_list").html(cityHtml);
}

function shopImpThumb(objcls)
{
	$("."+objcls+" img").each(function(){
		thumbPimg($(this).attr("id"),290,199);
	});
}

function thumbPimg(imgObjID,imgWidth,imgHeight)
{
	var toW = imgWidth;
	var toH = imgHeight;
	var ftoW = toW;
	var ftoH = toH;
	var imgObj = $("#"+imgObjID);			
	var imgsrc = imgObj.attr("imgsrc");
	var srcW = 0;
	var srcH = 0;
	
	var image = new Image();
	
	image.onload = function()
	{	
		if(srcW==0)
		{
			srcW = image.width;
			srcH = image.height;
		}
		if(srcW>0 && srcH>0)
		{
			flag=true;
			if(srcW/srcH>= toW/toH)
			{
				if(srcW>toW)
				{
					ftoW=toW;
					ftoH=(srcH*toW)/srcW;
				}
				else
				{
					if(srcH<=toH)
					{
						ftoW=srcW;
						ftoH=srcH;
					}
					else
					{
						ftoW=(srcW*toH)/srcH;
						ftoH=srcH;
					}
				}
			}
			else
			{
				if(srcH>toH)
				{
					ftoH=toH;
					ftoW=(srcW*toH)/srcH;
				}
				else
				{
					if(srcW<=toW)
					{
						ftoW=srcW;
						ftoH=srcH;
					}
					else
					{
						ftoW=toW;
						ftoH=srcH;
						ftoH=(srcH*toW)/srcW;
					}
				}
			}
		}
		imgObj.css({"width":ftoW,"height":ftoH}).attr("src",imgsrc).show();
	}	
	
	image.src = imgsrc;
}

function shopSearch(typeID)
{
	$("#t").val(typeID);
	$('#formSearch').submit();
}


